package com.berg.metroarrived;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Switch;
public class MainActivity extends Activity {
	private final static String DTAG = "metroarrived";
	SharedPreferences mPref = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//mTextStatus = (TextView)this.findViewById(R.id.tvStatus);
		
		//get preference
		mPref = getSharedPreferences(getPackageName(), MODE_PRIVATE);
				
		//enable service?
		boolean service_enabled = mPref.getBoolean("service_enabled", false);
		Switch sw  = (Switch)findViewById(R.id.swStart);
		sw.setChecked(service_enabled);
		Intent i = new Intent(this,MonitorService.class);
		Log.d(DTAG,"service_enabled "+ String.valueOf(service_enabled));
		if(service_enabled){
			startService(i);	
		}else{
			stopService(i);
		}
		
		LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		//List<String> providers = lm.getProviders(true);
		//for(String provider:providers){
		//	Log.d(DTAG,"provider:"+provider);
		//}
		Location loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		Log.d(DTAG,"my location is " +loc.toString());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onSwitchSerivce(View v){
		boolean service_enabled = ((Switch)v).isChecked();
		mPref.edit().putBoolean("service_enabled", service_enabled).apply();
		
		Intent i = new Intent(this,MonitorService.class);
		if(((Switch)v).isChecked()){
			startService(i);	
		}else{
			stopService(i);
		}
	}
	
	public void onListLocations(View v){
		Intent i = new Intent(this,StationsList.class);
		startActivity(i);
	}

}
